<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model="queryParams.vehicleType" placeholder="请选择车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.rental_sys_vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆颜色" prop="vehicleColor">
        <el-select v-model="queryParams.vehicleColor" placeholder="请选择车辆颜色" clearable>
          <el-option
            v-for="dict in dict.type.rental_sys_vehicle_color"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌和型号" prop="brandModel">
        <el-input
          v-model="queryParams.brandModel"
          placeholder="请输入品牌和型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每日租金" prop="rentalPricePerday">
        <el-input
          v-model="queryParams.rentalPricePerday"
          placeholder="请输入每日租金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用状态" prop="availability">
        <el-select v-model="queryParams.availability" placeholder="请选择可用状态" clearable>
          <el-option
            v-for="dict in dict.type.rental_sys_availability"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="vehicleYongList" @selection-change="handleSelectionChange">
      <el-table-column label="车辆ID" align="center" prop="vehicleId" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rental_sys_vehicle_type" :value="scope.row.vehicleType"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆颜色" align="center" prop="vehicleColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rental_sys_vehicle_color" :value="scope.row.vehicleColor"/>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="品牌和型号" align="center" prop="brandModel" />
      <el-table-column label="每日租金" align="center" prop="rentalPricePerday" />
      <el-table-column label="可用状态" align="center" prop="availability">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rental_sys_availability" :value="scope.row.availability"/>
        </template>
      </el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  <template slot-scope="scope">
    <!-- 判断可用状态，只有当可用状态为'0'时才显示按钮 -->
    <el-button
      v-if="scope.row.availability === '0'"
      size="mini"
      type="text"
      icon="el-icon-edit"
      @click="handleUpdate(scope.row)"
      v-hasPermi="['system:orders:add']"
    >我想租这辆</el-button>
  </template>
</el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加租赁订单 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-select v-model="form.vehicleType" placeholder="请选择车辆类型" disabled>
            <el-option
              v-for="dict in dict.type.rental_sys_vehicle_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆颜色" prop="vehicleColor">
          <el-select v-model="form.vehicleColor" placeholder="请选择车辆颜色" disabled>
            <el-option
              v-for="dict in dict.type.rental_sys_vehicle_color"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" disabled/>
        </el-form-item>
        <el-form-item label="品牌和型号" prop="brandModel">
          <el-input v-model="form.brandModel" placeholder="请输入品牌和型号" disabled/>
        </el-form-item>
        <el-form-item label="每日租金" prop="rentalPricePerday">
          <el-input v-model="form.rentalPricePerday" placeholder="请输入每日租金" disabled/>
        </el-form-item>
        <el-form-item label="可用状态" prop="availability">
          <el-select v-model="form.availability" placeholder="请选择可用状态" disabled>
            <el-option
              v-for="dict in dict.type.rental_sys_availability"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<el-form-item label="开始时间" prop="startTime">
  <el-date-picker
    v-model="form.startTime"
    type="date"
    value-format="yyyy-MM-dd"
    placeholder="请选择开始时间">
  </el-date-picker>
</el-form-item>
<el-form-item label="租赁天数" prop="contiTime">
  <el-input
    v-model="form.contiTime"
    placeholder="请输入租赁天数"
    clearable>
  </el-input>
</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交租赁订单申请</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicleYong, getVehicleYong, addOrders } from "@/api/system/vehicleYong";
import store from "@/store";
export default {
  name: "VehicleYong",
  dicts: ['rental_sys_vehicle_color', 'rental_sys_vehicle_type', 'rental_sys_availability'],
  data() {
    return {
      userId:store.state.userId,
      userName:store.state.userName,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 本公司车辆列表表格数据
      vehicleYongList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleType: null,
        vehicleColor: null,
        licensePlate: null,
        brandModel: null,
        rentalPricePerday: null,
        availability: null,
        vehicleId: null,
        startTime: null,
        contiTime: null,
        totalCost: null,
        paymentStatus: null,
        orderStatus: null
      },
      // 表单参数
      form: {
        startTime: null,
        contiTime: null,
      },
      // 表单校验
      rules: {
        vehicleType: [
          { required: true, message: "车辆类型不能为空", trigger: "change" }
        ],
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        brandModel: [
          { required: true, message: "品牌和型号不能为空", trigger: "blur" }
        ],
        rentalPricePerday: [
          { required: true, message: "每日租金不能为空", trigger: "blur" }
        ],
        availability: [
          { required: true, message: "可用状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    userId() {
      return this.$store.state.user.id;
    },
    userName() {
      return this.$store.state.user.name;
    }
  },
  methods: {
    /** 查询本公司车辆列表列表 */
    getList() {
      this.loading = true;
      listVehicleYong(this.queryParams).then(response => {
        this.vehicleYongList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
        // 重新从Vuex store中获取用户ID和用户名
        this.userId = this.$store.state.user.id;
        this.userName = this.$store.state.user.name;

      this.form = {
        vehicleType: null,
        vehicleColor: null,
        licensePlate: null,
        brandModel: null,
        rentalPricePerday: null,
        availability: null,
        vehicleId: null,
        startTime: null,
        contiTime: null,
        totalCost: null,
        paymentStatus: null,
        orderStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 租赁操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "租赁订单";
    },
/** 租赁操作 */
handleUpdate(row) {
  this.reset();
  this.form = {
    userId: this.userId, // 获取当前登录用户的ID
    userName: this.userName, // 获取当前登录用户的用户名
    vehicleId: row.vehicleId, // 获取选中车辆的ID
    brandModel: row.brandModel, // 获取选中车辆的品牌和型号
    rentalPricePerday: row.rentalPricePerday, // 获取每日租金
    startTime: null, // 开始时间需要用户输入
    contiTime: null, // 租赁天数需要用户输入
    paymentStatus: '00', // 默认设置为“未支付（默认）”
    orderStatus: '00', // 默认设置为“未开始（业务员未审核）”
    vehicleType: row.vehicleType,
    vehicleColor: row.vehicleColor,
    licensePlate: row.licensePlate,
    availability: row.availability
  };
  this.open = true;
  this.title = "租赁订单";
},
  /** 提交按钮 */
submitForm() {
  this.$refs["form"].validate(valid => {
    if (valid) {
      // 计算总租金
      this.form.totalCost = this.form.rentalPricePerday * this.form.contiTime;

      // 调用后端API添加租赁订单
      addOrders(this.form).then(response => {
        this.$modal.msgSuccess("提交成功，请移步至我的订单查看");
        this.open = false;
        this.getList(); // 刷新车辆列表
      }).catch(error => {
        console.error(error);
        this.$message.error("提交失败，请稍后重试");
      });
    } else {
      // 表单验证失败的处理逻辑
      return false;
    }
  });
}


  }
};
</script>
